const routerNav = (path, params = {}) => {
	const queryString = Object.entries(params)
		.map(([key, value]) => `${key}=${value}`)
		.join('&');
	uni.navigateTo({
		url: path + (queryString ? '?' + queryString : '')
	});
};
const showToast = (title) => {
	uni.showToast({
		title,
		icon: 'none',
		duration: 1000
	});
}
const getStorageSync = (key) => {
	return uni.getStorageSync(key)
}
const setStorageSync = (key, data) => {
	return uni.setStorageSync(key, data)
}
const removeStorageSync = (key) => {
	return uni.removeStorageSync(key)
}
const computedHeight = (currentThis, node, offSet = 0) => {
	return new Promise((resolve, reject) => {
		const height = uni.getSystemInfoSync().windowHeight;
		const topEl = uni.createSelectorQuery().in(currentThis).select(node);
		topEl.boundingClientRect((data) => {
			if (data) {
				const scrollHeight = height - data.height - offSet;
				resolve(scrollHeight);
			} else {
				const scrollHeight = height - offSet;
				resolve(scrollHeight);
			}
		}).exec();
	});
}
const uploadImage = (uploadUrl, path) => {
	let token = uni.getStorageSync('token');
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: uploadUrl,
			filePath: path,
			name: 'file',
			header: {
				'token': token,
			},
			success: (res) => {
				resolve(JSON.parse(res.data).data.url)
			}
		});
	})
}

const checkForm = (formList) => {
	return new Promise((resolve, reject) => {
		for (const item of formList) {
			const [type, value, label] = item;
			const isValid = (type === 'boolean' && Boolean(value)) ||
				(type === 'string' && value) ||
				(type === 'array' && value.length > 0);
			if (!isValid) {
				const nullstr = uni.getStorageSync('lang') == 'zh-cn' ? "不能为空" : "it cannot be empty"
				uni.showToast({
					title: `${label}` + " " + nullstr,
					duration: 1000,
					icon: 'none'
				});
				return;
			}
		}
		resolve();
	});
};
const getUserConfig = () => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: "加载中",
			mask: true,
		});
		uni.getSetting({
			success(res) {
				if (!res.authSetting['scope.userFuzzyLocation']) {
					uni.authorize({
						scope: 'scope.userFuzzyLocation',
						success() {
							uni.getFuzzyLocation({
								type: 'wgs84',
								success: (res) => {
									resolve({
										code: 200,
										location: {
											lng: res.longitude,
											lat: res.latitude
										}
									})
								}
							})
						},
						fail(error) {
							uni.showModal({
								title: '提示',
								content: '若点击不授权，将无法使用位置功能',
								cancelText: '不授权',
								cancelColor: '#999',
								confirmText: '授权',
								confirmColor: '#f94218',
								success(res) {
									if (res.confirm) {
										uni.openSetting({
											success(res) {
												console.log(res.authSetting)
											}
										})
									} else if (res.cancel) {
										resolve({
											code: -1,
										})
									}
								}
							})
						}
					})
				} else {
					uni.getFuzzyLocation({
						type: 'wgs84',
						success: (res) => {
							resolve({
								code: 200,
								location: {
									lng: res.longitude,
									lat: res.latitude
								}
							})
						}
					})
				}
			}
		})
		uni.hideLoading()
	})
}
const handleImage = (url) => {
	// console.log("https://www.slem.com.au' + url", 'https://www.slem.com.au' + url)
	return 'https://www.slem.com.au' + url
}
const openLocation = (lat, lon) => {
	uni.openLocation({
		latitude: lat * 1,
		longitude: lon * 1,
	});
}
const makePhoneCall = (phoneNumber) => {
	uni.makePhoneCall({
		phoneNumber
	});
}
const requestPayment = (payInfo) => {
	return new Promise((resolve) => {
		uni.requestPayment({
			provider: 'wxpay',
			timeStamp: payInfo.timeStamp,
			nonceStr: payInfo.nonceStr,
			package: payInfo.package,
			signType: payInfo.signType,
			paySign: payInfo.paySign,
			success: async (res) => {
				uni.showToast({
					title: '支付成功',
					icon: 'none',
					duration: 1000
				})
				await delay(1000)
				resolve()
			},
			fail: (err) => {
				uni.showToast({
					title: '取消支付',
					icon: 'error',
					duration: 1000
				})
			}
		});
	})
}
const delay = (ms, callback) => {
	return new Promise((resolve) => {
		setTimeout(() => {
			callback()
			resolve()
		}, ms)
	});
}
const showCustomModal = (content, callBack) => {
	return new Promise((resolve) => {
		uni.showModal({
			title: '温馨提示',
			content: content,
			success: (res) => {
				if (res.confirm) {
					callBack()
				} else if (res.cancel) {
					resolve(false)
				}
			}
		});
	})
}
export default {
	routerNav,
	showToast,
	getStorageSync,
	setStorageSync,
	removeStorageSync,
	computedHeight,
	uploadImage,
	handleImage,
	checkForm,
	getUserConfig,
	openLocation,
	makePhoneCall,
	requestPayment,
	delay,
	showCustomModal
}